package com.txunda.zhuohired.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.txunda.zhuohired.R;
import com.txunda.zhuohired.util.wheel.NumericWheelAdapter;
import com.txunda.zhuohired.util.wheel.OnWheelChangedListener;
import com.txunda.zhuohired.util.wheel.OnWheelScrollListener;
import com.txunda.zhuohired.util.wheel.WheelView;

import java.util.Calendar;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2017/11/13 09:49
 * 功能描述：选择生日Dialog
 * 联系方式：jiahuanchen@126.com
 */

public class DateDialog extends Dialog implements View.OnClickListener {
    private SelectBirthdayListener selectBirthdayListener;
    private WheelView wl_dialog_birthday_year;//年
    private WheelView wl_dialog_birthday_month;//月
    private WheelView wl_dialog_birthday_day;//日
    private Context context;
    private int curYear;
    private int curMonth;
    private String date = "";
    private TextView tv_dialog_birthday_tittle;
    private String dialogTittle;

    public DateDialog(Context context, String dialogTittle, SelectBirthdayListener
            selectBirthdayListener) {
        super(context, R.style.dialog_style);
        this.context = context;
        this.selectBirthdayListener = selectBirthdayListener;
        this.dialogTittle = dialogTittle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_birthday);
        wl_dialog_birthday_year = findViewById(R.id.wl_dialog_birthday_year);
        wl_dialog_birthday_month = findViewById(R.id.wl_dialog_birthday_month);
        wl_dialog_birthday_day = findViewById(R.id.wl_dialog_birthday_day);
        tv_dialog_birthday_tittle = findViewById(R.id.tv_dialog_birthday_tittle);
        findViewById(R.id.tv_dialog_birthday_cancel).setOnClickListener(this);
        findViewById(R.id.tv_dialog_birthday_sure).setOnClickListener(this);
        initWheelView();
        tv_dialog_birthday_tittle.setText(dialogTittle);
    }

    @Override
    public void onClick(View view) {
        this.dismiss();
        switch (view.getId()) {
            case R.id.tv_dialog_birthday_sure:
                selectBirthdayListener.onSelectBirthday(date);
                break;
        }
    }

    public interface SelectBirthdayListener {
        void onSelectBirthday(String birthday);
    }

    private void initWheelView() {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        int curDate = c.get(Calendar.DATE);
        String month = "";
        String day = "";
        if (curMonth < 10) {
            month = "0" + curMonth;
        } else {
            month = "" + curMonth;
        }
        if (curDate < 10) {
            day = "0" + curDate;
        } else {
            day = "" + curDate;
        }
        date = curYear + "/" + month + "/" + day;


        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(context, 2000, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_dialog_birthday_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(20);
        wl_dialog_birthday_year.setCyclic(true);//是否可循环滑动
        wl_dialog_birthday_year.addScrollingListener(startScrollListener);
        wl_dialog_birthday_year.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curYear = newValue + 2000;
                initStartDayAdapter();
            }
        });

        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(context, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_dialog_birthday_month.setViewAdapter(numericWheelAdapterStart2);
        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(20);
        wl_dialog_birthday_month.setCyclic(true);
        wl_dialog_birthday_month.addScrollingListener(startScrollListener);
        wl_dialog_birthday_month.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curMonth = newValue + 1;
                initStartDayAdapter();
            }
        });
        initStartDayAdapter();


        wl_dialog_birthday_year.setCurrentItem(curYear - 2000);
        wl_dialog_birthday_month.setCurrentItem(curMonth - 1);
        wl_dialog_birthday_day.setCurrentItem(curDate - 1);


    }

    private void initStartDayAdapter() {
        NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(context, 1, getDay(curYear, curMonth)
                , "%02d");
        numericWheelAdapterStart3.setLabel("日");

        wl_dialog_birthday_day.setViewAdapter(numericWheelAdapterStart3);
        numericWheelAdapterStart3.setTextColor(R.color.black);
        //        numericWheelAdapterStart3.setBackGroundColor(R.color.colorPrimary);
        numericWheelAdapterStart3.setTextSize(20);
        wl_dialog_birthday_day.setCyclic(true);
        wl_dialog_birthday_day.addScrollingListener(startScrollListener);
    }

    OnWheelScrollListener startScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = wl_dialog_birthday_year.getCurrentItem() + 2000;//年
            int n_month = wl_dialog_birthday_month.getCurrentItem() + 1;//月
            int n_day = wl_dialog_birthday_day.getCurrentItem() + 1;//日
            String month = "";
            String day = "";
            if (n_month < 10) {
                month = "0" + n_month;
            } else {
                month = "" + n_month;
            }
            if (n_day < 10) {
                day = "0" + n_day;
            } else {
                day = "" + n_day;
            }
            date = n_year + "/" + month + "/" + day;
        }
    };

    /**
     * 根据年月获得 这个月总共有几天
     *
     * @param year
     * @param month
     * @return
     */
    public int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

}
